<?php
/**
 * 修复内容关联的wiki数量
 * @author tangyiwan
 */

ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\module\Classes\wiki\RelateWiki as clsRelateWiki;
use Doraemon\cache\ArticleCounter;
use Doraemon\model\Article as mArticle;

$uid = 0;
if (isset($argv[1]) && isset($argv[2])) {
    $objId = $argv[1];
    $objType = $argv[2];
    clsRelateWiki::setWikisNumByObjId($objId, $objType);
} else {
    $page = 1;
    $pageSize = 2000;
    $list = [];
    do {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $list = mObjRelateWiki::select(['obj_id', 'obj_type'], [
            'status' => mObjRelateWiki::STATUS_NORMAL,
            'wiki_id[>]' => 0,
            'LIMIT' => $limit,
            'GROUP' => ['obj_id', 'obj_type'],
        ]);
        foreach ($list as $row) {
            var_dump('更新内容：' . $row['obj_id']);
            clsRelateWiki::setWikisNumByObjId($row['obj_id'], $row['obj_type']);
        }
        $page++;
    } while ($list);


//修复整屋 关联wiki数量
    $articleIds = [];
    $page = 1;
    $pageSize = 2000;
    $objType = mObjRelateWiki::OBJ_TYPE_ARTICLE;
    $i = 0;
    do {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $articleIds = mArticle::select('aid', ['status' => [1, 5], 'LIMIT' => $limit]);
        var_dump(mArticle::log());
        foreach ($articleIds as $articleId) {
            $counter = ArticleCounter::getOne($articleId);
            if (!empty($counter) && isset($counter['wiki']) && $counter['wiki']) {
                var_dump($i . '==' . $articleId . ":" . $counter['wiki']);
                clsRelateWiki::setWikisNumByObjId($articleId, $objType);
            }
            $i++;
        }
        $page++;
    } while ($articleIds);
}